<?
require_once('config.php');
$action = getValue('action','str','POST');
if($action == 'submit'){
   $cat_id   = getValue('cat_id','int','POST');
   $pla_id   = getValue('pla_id','int','POST');
   $vid_app  = getValue('vid_app','int','POST');
   $vid_type = getValue('vid_type','int','POST',0);
   $link_playlist = getValue('link_playlist','str','POST','');
   $start_index  = getValue('start_index','int','POST',0);
   $max_results  = getValue('max_results','int','POST',0);
   $received     = getValue('has_get','int','POST',0);
   $array_videos = array();
   $respone = YoutubeApi::getMovieFromPlayList($link_playlist,$start_index,$max_results);
   if($respone['code'] == 200){
      $array_videos = $respone['data'];
   }else{
      echo '<span style="color: red;font-size: 14px;font-weight: bold;">Có lỗi xảy ra</span>';
   }
   if(count($array_videos) > 0){
      $value_insert = '';
      $sql_insert   = '';
      $rows_inserted = 0;
      foreach($array_videos as $value){
         $vid_id_youtube = $value['id'];
         $db_count = new db_count($sql = "SELECT vid_id FROM video WHERE vid_id_youtube='" . $vid_id_youtube."'");         
         if($db_count->total == 0){
            $vid_name  = handelVidName($value['title']);
            $vid_lyric = '';
            $vid_duration = formatDuraction($value['duration']);
            $vid_image    = 'http://i1.ytimg.com/vi/'.$vid_id_youtube.'/mqdefault.jpg';
            $value_insert .= "('" . $vid_name . "',
                               " . $cat_id . ",
                               " . $pla_id . ",
                               " . $vid_app . ",
                               '" . $vid_duration . "',
                               '" . $vid_image . "',
                               " . time() . ",
                               '" . $vid_id_youtube . "',
                               " . $vid_type . "),";   
         }
      }
      if($value_insert != ''){
         $value_insert = substr($value_insert,0,-1); 
         $sql_insert = "INSERT INTO 
                        video(vid_name,
                              cat_id,
                              pla_id,
                              vid_app,
                              vid_duration,
                              vid_image,
                              vid_date,
                              vid_id_youtube,
                              vid_type)
                        VALUES
                           " . $value_insert;  
      }
      if($sql_insert != ''){   
         $db_exe = new db_execute($sql_insert);
         $rows_inserted = $db_exe->rows_affected;
         $received += $rows_inserted;
      }
      
      

   }
   if($rows_inserted > 0)
      echo '<span style="color: #01B42C;font-size: 14px;font-weight: bold;">Thêm thành công '.$rows_inserted.' videos </span>';
   else
      echo '<span style="color: red;font-size: 14px;font-weight: bold;">Có thể video này đã tồn tại</span>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thêm mới <?=$moduol_name?></title>
   <?=$css_global?>
   <?=$js_global?>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $(document).on('click','.reload_tab' ,function(){
      window.location.reload();
    });
  });
  </script>
</head>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Thêm mới <?=$moduol_name?></h1>      
      <div class="reload_tab" title="Reload"></div>                         
      <div class="clear"></div>
   </div>
   <div class="block-fluid">
      <form class="form-horizontal" name="add" action="" method="POST" enctype="multipart/form-data"  onsubmit="">
         <input type="hidden" name="action" value="submit" />
         <fieldset>
            <div class="control-group">
               <label class="control-label" for="input01">Link playlist(Youtube)</label>
               <div class="controls">
						<input type="text" value="<?=isset($link_playlist) ? $link_playlist : ''?>" class="input-xlarge tooltip-right span5" id="link_playlist" name="link_playlist" title="Link playlist"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Loại nhạc</label>
               <div class="controls">
                  <select id="vid_type" name="vid_type">
                     <option value="0">Không lời</option>
                     <option value="1">Có lời</option>
                  </select>
               </div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Từ video</label>
               <div class="controls">
						<input type="text" value="<?=isset($received) ? $received  + 1 : 1?>" class="input-xlarge tooltip-right span3" id="start_index" name="start_index" title="Vị trí video bắt đầu lấy"/>
					</div>
               
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Tổng số</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span3" id="max_results" name="max_results" title="Tổng số video muốn lấy"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Đã lấy từ playlist</label>
               <div class="controls">
						<input type="text" value="<?=isset($received) ? $received : 0?>" class="input-xlarge tooltip-right span3" id="has_get" name="has_get" readonly="true" title="Video đã lấy từ channel"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn thể loại</label>
               <div class="controls">
						<select id="cat_id" name="cat_id">
                     <option value="0">Không chọn</option>
                     <?if($count_cat > 0):?>
                        <?foreach($array_all_cat as $key=>$value):?>
                           <option value="<?=$key?>"><?=$value?></option>
                        <?endforeach?>
                     <?endif?>
                  </select>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn playlist</label>
               <div class="controls">
               <?php if(count($arr_pla) > 0){?>
                  <select id="pla_id" name="pla_id">
                     <option value="0">Không chọn</option>
                        <?foreach($arr_pla as $value):?>
                           <option value="<?=$value['pla_id']?>"><?=$value['pla_name']?></option>
                        <?endforeach?>
                  </select>
               <?php }?>
               </div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn App</label>
               <div class="controls">
               <?php if(count($arry_app) > 0){?>
                  <select id="vid_app" name="vid_app">
                        <?foreach($arry_app as $key=>$value):?>
                           <option value="<?=$key?>"><?=$value?></option>
                        <?endforeach?>
                  </select>
               <?php }?>
               </div>
            </div>
            <div class="form-actions">
					<button type="submit" class="btn btn-info">Thêm</button>
					<button type="reset"  class="btn btn-warning">Làm lại</button>
				</div>
         </fieldset>
      </form>
   </div> 
</div>